package com.ruoyi.service.achievements.impl;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.mapper.achievements.AcvScoringDataManageMapper;
import com.ruoyi.mapper.achievements.AcvTemplateManageMapper;
import com.ruoyi.mapper.achievements.AcvTwoWayMapper;
import com.ruoyi.service.achievements.AcvScoringDataManageService;
import com.ruoyi.service.achievements.AcvTwoWayService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zeo
 * @explain 考勤数据服务实现类
 * @date 2020/8/25 17:34
 */
@Service
public class AcvScoringDataManageServiceImpl implements AcvScoringDataManageService {

    @Autowired
    AcvScoringDataManageMapper acvScoringDataManageMapper;

    @Autowired
    AcvTemplateManageMapper acvTemplateManageMapper;

    @Autowired
    AcvTwoWayMapper acvTwoWayMapper;

    @Override
    public List<Map<String, Object>> findScoringGdData(Map map) {
        map.put("superId", ShiroUtils.getSysUser().getUserId());
        map.put("superName", ShiroUtils.getSysUser().getLoginName());
        List<Map<String, Object>> mapList = acvScoringDataManageMapper.findScoringGdData(map);

        //双向打分综合得分计算
        for (Map map1 : mapList) {
            map1.put("twoWayScore", getTwoWayScore(map1));
        }
        return mapList;
    }

    @Override
    public List<Map<String, Object>> findGdData(String examineeId, String years, String months) {
        return acvScoringDataManageMapper.findGdData(examineeId, years, months);
    }

    @Override
    public AjaxResult exportSummary(String years, String months, String name, String badge, String deptId, String jobId) {
        Map map = new HashMap();
        map.put("years", years);
        map.put("months", months);
        map.put("name", name);
        map.put("badge", badge);
        map.put("deptId", deptId);
        map.put("jobId", jobId);
        List<Map<String, Object>> list = this.findScoringGdData(map);
        try {
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet("绩效汇总表");
            //新增一行
            HSSFRow row = sheet.createRow(0);
            HSSFCellStyle txtStyle1 = wb.createCellStyle();
            HSSFFont txtFont = wb.createFont();
            txtFont.setFontName("宋体");
            txtFont.setFontHeightInPoints((short) 11);
            txtFont.setBold(true);
            txtStyle1.setFont(txtFont);
            txtStyle1.setWrapText(true);
            txtStyle1.setAlignment(HorizontalAlignment.CENTER);
            txtStyle1.setVerticalAlignment(VerticalAlignment.CENTER);
            txtStyle1.setBorderBottom(BorderStyle.THIN);
            txtStyle1.setBorderLeft(BorderStyle.THIN);
            txtStyle1.setBorderTop(BorderStyle.THIN);
            txtStyle1.setBorderRight(BorderStyle.THIN);
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("工号");
            row.createCell(2).setCellValue("姓名");
            row.createCell(3).setCellValue("二级部门");
            row.createCell(4).setCellValue("三级部门");
            row.createCell(5).setCellValue("岗位");
            row.createCell(6).setCellValue("年份");
            row.createCell(7).setCellValue("月份");
            row.createCell(8).setCellValue("考核人");
            row.createCell(9).setCellValue("考核得分");
            row.createCell(10).setCellValue("备注");
            row.createCell(11).setCellValue("双向综合得分");

            row.setHeightInPoints(25);

            for (Cell cell : row) {
                cell.setCellStyle(txtStyle1);
            }


            HSSFCellStyle txtStyle = wb.createCellStyle();
            HSSFFont txtFont1 = wb.createFont();
            txtFont1.setFontName("宋体");
            txtFont1.setFontHeightInPoints((short) 9);
            txtStyle.setFont(txtFont1);
            txtStyle.setWrapText(true);
            txtStyle.setAlignment(HorizontalAlignment.CENTER);
            txtStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            txtStyle.setBorderBottom(BorderStyle.THIN);
            txtStyle.setBorderLeft(BorderStyle.THIN);
            txtStyle.setBorderTop(BorderStyle.THIN);
            txtStyle.setBorderRight(BorderStyle.THIN);


            for (int i = 0; i < list.size(); i++) {
                String twoWayScore = getTwoWayScore(list.get(i));
                if (twoWayScore == null) {
                    twoWayScore = "";
                }
                list.get(i).put("twoWayScore", twoWayScore);

                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(i + 1);
                row.createCell(1).setCellValue(list.get(i).get("badge").toString());
                row.createCell(2).setCellValue(list.get(i).get("examineeName").toString());
                row.createCell(3).setCellValue(list.get(i).get("deptName").toString());
                row.createCell(4).setCellValue(list.get(i).get("tertiaryDeptName").toString());
                row.createCell(5).setCellValue(list.get(i).get("jobName").toString());
                row.createCell(6).setCellValue(list.get(i).get("years").toString());
                row.createCell(7).setCellValue(list.get(i).get("months").toString());
                row.createCell(8).setCellValue(list.get(i).get("scoringName").toString());
                row.createCell(9).setCellValue(list.get(i).get("scoreTotal").toString());
                row.createCell(10).setCellValue(list.get(i).get("remarksTotal").toString());
                row.createCell(11).setCellValue(list.get(i).get("twoWayScore").toString());

                row.setHeightInPoints(25);
                for (Cell cell : row) {
                    cell.setCellStyle(txtStyle);
                }
            }


            sheet.setColumnWidth(0, 23 * 256);
            sheet.setColumnWidth(1, 23 * 256);
            sheet.setColumnWidth(2, 23 * 256);
            sheet.setColumnWidth(3, 23 * 256);
            sheet.setColumnWidth(4, 23 * 256);
            sheet.setColumnWidth(5, 23 * 256);
            sheet.setColumnWidth(6, 23 * 256);
            sheet.setColumnWidth(8, 23 * 256);
            sheet.setColumnWidth(9, 23 * 256);
            sheet.setColumnWidth(10, 30 * 256);
            sheet.setColumnWidth(11, 23 * 256);


            String filePath = Global.getDownloadPath() + "绩效汇总表.xls";
            FileOutputStream fileOut = new FileOutputStream(filePath);
            wb.write(fileOut);
            fileOut.close();
            return AjaxResult.success(0);

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("失败");
        }
    }

    @Override
    public AjaxResult exportDetailed(String scoringId) {
        Map map = acvScoringDataManageMapper.findScoringGdByScoringId(scoringId);
        String[] templateArray = map.get("template_id").toString().split(",");
        String[] remarkArray = map.get("remarks").toString().split("@@");
        String[] scoreArray = map.get("score").toString().split(",");
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < templateArray.length; i++) {
            String templateId = templateArray[i];
            Map map1 = acvTemplateManageMapper.findTemplateById(templateId);
            map1.put("remarks", remarkArray[i]);
            map1.put("score", scoreArray[i]);
            map1.put("badge", map.get("badge"));
            map1.put("examineeName", map.get("examineeName"));
            map1.put("userName", map.get("userName"));
            list.add(map1);
        }
        try {
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet("绩效明细表");
            //新增一行
            HSSFRow row = sheet.createRow(0);
            HSSFCellStyle txtStyle1 = wb.createCellStyle();
            HSSFFont txtFont = wb.createFont();
            txtFont.setFontName("宋体");
            txtFont.setFontHeightInPoints((short) 11);
            txtFont.setBold(true);
            txtStyle1.setFont(txtFont);
            txtStyle1.setWrapText(true);
            txtStyle1.setAlignment(HorizontalAlignment.CENTER);
            txtStyle1.setVerticalAlignment(VerticalAlignment.CENTER);
            txtStyle1.setBorderBottom(BorderStyle.THIN);
            txtStyle1.setBorderLeft(BorderStyle.THIN);
            txtStyle1.setBorderTop(BorderStyle.THIN);
            txtStyle1.setBorderRight(BorderStyle.THIN);
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("工号");
            row.createCell(2).setCellValue("姓名");
            row.createCell(3).setCellValue("考核指标");
            row.createCell(4).setCellValue("指标描述/计算公式");
            row.createCell(5).setCellValue("达成目标");
            row.createCell(6).setCellValue("评分标准");
            row.createCell(7).setCellValue("指标配分(满分100分)");
            row.createCell(8).setCellValue("考核人");
            row.createCell(9).setCellValue("考核得分");
            row.createCell(10).setCellValue("备注");

            row.setHeightInPoints(27);
            for (Cell cell : row) {
                cell.setCellStyle(txtStyle1);
            }

            HSSFCellStyle txtStyle = wb.createCellStyle();
            txtStyle.setWrapText(true);
            txtStyle.setAlignment(HorizontalAlignment.CENTER);
            txtStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            txtStyle.setBorderBottom(BorderStyle.THIN);
            txtStyle.setBorderLeft(BorderStyle.THIN);
            txtStyle.setBorderTop(BorderStyle.THIN);
            txtStyle.setBorderRight(BorderStyle.THIN);


            for (int i = 0; i < list.size(); i++) {
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(i + 1);
                row.createCell(1).setCellValue(list.get(i).get("badge").toString());
                row.createCell(2).setCellValue(list.get(i).get("examineeName").toString());
                row.createCell(3).setCellValue(list.get(i).get("assessmentIndex").toString());
                row.createCell(4).setCellValue(list.get(i).get("describe").toString());
                row.createCell(5).setCellValue(list.get(i).get("goal").toString());
                row.createCell(6).setCellValue(list.get(i).get("standard").toString());
                row.createCell(7).setCellValue(list.get(i).get("distribution").toString());
                row.createCell(8).setCellValue(list.get(i).get("userName").toString());
                row.createCell(9).setCellValue(list.get(i).get("score").toString());
                row.createCell(10).setCellValue(list.get(i).get("remarks").toString());
                row.setHeightInPoints(25);
                for (Cell cell : row) {
                    cell.setCellStyle(txtStyle);
                }
            }

            int listSize = list.size() + 1;
            row = sheet.createRow(listSize);
            sheet.addMergedRegion(new CellRangeAddress(listSize, listSize, 0, 6));
            row.createCell(0).setCellValue("小计");
            row.createCell(1);
            row.createCell(2);
            row.createCell(3);
            row.createCell(4);
            row.createCell(5);
            row.createCell(6);
            int distributionSum = 0;
            for (int i = 0; i < list.size(); i++) {
                distributionSum += Double.parseDouble(list.get(i).get("distribution").toString());
            }
            row.createCell(7).setCellValue(distributionSum);
            row.createCell(8);
            row.createCell(9).setCellValue(map.get("score_total").toString());
            row.createCell(10).setCellValue(map.get("remarksTotal").toString());
            row.setHeightInPoints(25);
            for (Cell cell : row) {
                cell.setCellStyle(txtStyle1);
            }

            sheet.setColumnWidth(0, 15 * 256);
            sheet.setColumnWidth(1, 15 * 256);
            sheet.setColumnWidth(2, 15 * 256);
            sheet.setColumnWidth(3, 20 * 256);
            sheet.setColumnWidth(4, 30 * 256);
            sheet.setColumnWidth(5, 23 * 256);
            sheet.setColumnWidth(6, 30 * 256);
            sheet.setColumnWidth(7, 20 * 256);
            sheet.setColumnWidth(8, 15 * 256);
            sheet.setColumnWidth(9, 15 * 256);
            sheet.setColumnWidth(10, 20 * 256);

            String filePath = Global.getDownloadPath() + "绩效明细表.xls";
            FileOutputStream fileOut = new FileOutputStream(filePath);
            wb.write(fileOut);
            fileOut.close();
            return AjaxResult.success(0);

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("失败");
        }
    }

    @Override
    public void dataMigration() {
        List<Map<String, Object>> list = acvScoringDataManageMapper.aaa();
        List<Map<String, Object>> list2 = new ArrayList<>();
        for (Map map : list) {
            List<Map<String, Object>> list1 = acvScoringDataManageMapper.bbb(map.get("examinee_id").toString(), map.get("years").toString(), map.get("months").toString());
            String templateIdList = "";
            String scoreList = "";
            String remarkList = "";
            float scoreTotal = 0;
            for (Map map1 : list1) {
                String scoreStr = map1.get("score").toString();

                if (scoreStr.substring(scoreStr.length() - 1, scoreStr.length()).equals("0")) {
                    int score = Integer.parseInt(map1.get("score").toString().substring(0, map1.get("score").toString().length() - 2));
                    scoreTotal += Integer.parseInt(map1.get("score").toString().substring(0, map1.get("score").toString().length() - 2));
                    scoreList += score + ",";
                } else {
                    double score = Double.parseDouble(map1.get("score").toString());
                    scoreTotal += score;
                    scoreList += score + ",";
                }
                templateIdList += map1.get("template_id") + ",";

                if (map1.get("remarks").toString() == null || map1.get("remarks").toString().equals("")) {
                    remarkList += "无@@";
                } else {
                    remarkList += map1.get("remarks") + "@@";
                }
            }
            Map map1 = new HashMap();
            map1.put("templateIdList", templateIdList.substring(0, templateIdList.length() - 1));
            map1.put("scoreList", scoreList.substring(0, scoreList.length() - 1));
            map1.put("remarkList", remarkList.substring(0, remarkList.length() - 2));
            map1.put("years", map.get("years"));
            map1.put("months", map.get("months"));
            map1.put("scoreTotal", scoreTotal);
            map1.put("examineeId", map.get("examinee_id"));
            acvScoringDataManageMapper.ccc(map1);
        }

        System.out.println("aa");
    }

    @Override
    public int back(String scoringId) {
        if (acvScoringDataManageMapper.back(scoringId) > 0) {
            return acvScoringDataManageMapper.deleteGd(scoringId);
        } else {
            return 0;
        }
    }

    @Override
    public int backSubmit(String scoringId) {
        if (acvScoringDataManageMapper.backSubmit(scoringId) > 0) {
            return acvScoringDataManageMapper.deleteBack(scoringId);
        } else {
            return 0;
        }
    }

    public String getTwoWayScore(Map map) {
        String twoWayScore = "";
        DecimalFormat df1 = new DecimalFormat("0.0");

        Map map2 = acvTwoWayMapper.findTwoWayObj(map.get("examineeName").toString(), map.get("badge").toString());
        if (map2 == null) {
            return null;
        }

        //先找职能部门的分数 * 占比,再找项目部的分数 * 占比

        BigDecimal functionScore = acvTwoWayMapper.findTwoWayDeptScoreByDeptId(map2.get("name").toString(), map2.get("badge").toString(), map.get("years").toString(), map.get("months").toString(), map2.get("function_dept_id").toString());
        BigDecimal proScore = acvTwoWayMapper.findTwoWayDeptScoreByDeptId(map2.get("name").toString(), map2.get("badge").toString(), map.get("years").toString(), map.get("months").toString(), map2.get("pro_dept_id").toString());
        if (functionScore != null && proScore != null) {
            functionScore = functionScore.multiply(new BigDecimal(map2.get("function_proportion").toString()));
            proScore = proScore.multiply(new BigDecimal(map2.get("pro_proportion").toString()));
            //相加
            twoWayScore = df1.format(functionScore.add(proScore));
            return twoWayScore;
        }
        return twoWayScore;
    }


    public static void main(String[] args) {
        System.out.println(Double.parseDouble("0.5"));
    }
}
